package com.zhongketech.sunflower;


import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.database.ApplicationData;
import com.zhongketech.sunflower.database.Data;
import com.zhongketech.sunflower.database.PhoneNumberData;
import com.zhongketech.sunflower.util.Constants;
import com.zhongketech.sunflower.util.Root;
import com.zhongketech.sunflower.util.SystemUiHider;
import com.zhongketech.sunflower.util.Utilities;
import com.zhongketech.sunflower.view.LogUpdate;
import com.zhongketech.sunflower.view.PhoneNumList;
import com.zhongketech.sunflower.view.UninstallList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;



public class PhoneNumActivity extends BaseActivity {

	private final static String TAG = "PhoneNumActivity";
	private static Context context = null;

	private MyHandler myHandler = null;

	/**
	 * View init
	 */
	private ListView listView = null;
	private EditText editText = null;
	private Button button = null;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.filter_phonenum);

		//custom
		myHandler = new MyHandler();
		initView();
		context.startService(new Intent("com.zhongketech.sunflower.filter.SMSSERVICES"));

	}

	private void initView()
	{
		context = this;
		listView = (ListView) findViewById(R.id.numlistview);
		listView.setAdapter(PhoneNumList.getInstance(this).getAdapter());
		listView.setOnItemClickListener(listOnClick);
		editText = (EditText) findViewById(R.id.numEdit);
		button = (Button) findViewById(R.id.numButton);
		button.setOnClickListener(onClickListener);
	}

	private  android.widget.AdapterView.OnItemClickListener listOnClick= new android.widget.AdapterView.OnItemClickListener()
	{
		int clicks = 0;
		int lastPostion = -1;

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Utilities.logCat(TAG, "parent: "+parent+" view:"+view+" position: "+position+" id: "+id, Constants.LOG_ONLYSHELL);
			if(position == lastPostion || -1 == lastPostion)
			{
				lastPostion = position;
				clicks ++;
			}
			else
			{
				lastPostion = -1;
				clicks = 0;
			}



			if(clicks == 2)
			{
				Data.getinstance().del(position, Data.DATATYPE_PHONEBLACKLIST);
				lastPostion = -1;
				clicks = 0;
				return;

			}

			Toast.makeText(getApplicationContext(), "再点击 一次删除:  "+position,
					Toast.LENGTH_SHORT).show();

		}

	};

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {  
			// TODO Auto-generated method stub
			String input = editText.getText().toString();
			String name = null;
			String num = null;


			//如果输入的字符已点分割，则认为输入的是包名，否则，认为输入的应用名字
			if(null != input && input.matches("[0-9]+"))
			{
				num = input;
				Utilities.logCat(TAG, "add num: "+input, Constants.LOG_INFO);
			}
			else
			{
				Toast.makeText(getApplicationContext(), "非法输入，请输入电话号码",
						Toast.LENGTH_SHORT).show();
				return;
			}

			boolean addRet = Data.getinstance().add(new PhoneNumberData(num,name),Data.DATATYPE_PHONEBLACKLIST);
			//界面显示
			if(addRet && null != myHandler )
			{
				Message m = new Message();
				m.what = Constants.UPDATE_PHONENUM;
				m.obj = input;
				myHandler.sendMessage(m);
				editText.setText("");
			}
			else
			{
				Toast.makeText(getApplicationContext(), "数据已存在",
						Toast.LENGTH_SHORT).show();
			}

		}
	};

	/**
	 * 数据 更新操作
	 */
	static class MyHandler extends Handler
	{


		@Override
		public void handleMessage(Message msg) 
		{

			//Utilities.logCat(TAG, "msg.what "+msg.what+" msg.arg1: "+msg.arg1+" msg.arg2: "+msg.arg2);
			switch (msg.what) {
			case Constants.UPDATE_PHONENUM:
				//更新界面
				PhoneNumList.getInstance(context).update();
				break;

			default:
				break;
			}
		}

	}
}
